package com.blockchain.anti_counterfeiting.auth;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/desktop/home/auth/VerifyToken")
public class VerifyToken extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        doPost(req, res);
    }
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        try {
            PrintWriter out = res.getWriter();
            res.setContentType("application/json");
            String token = req.getParameter("token");
            Auth a = new Auth(token);
            if (a.verifyToken()){
                res.setStatus(HttpServletResponse.SC_OK);
            } else {
                out.println("{\"url\":\"" + req.getContextPath() + "/" + "\"}");
                res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            }
            destroy();
        }catch(Exception e){
            res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            destroy();
        }

    }
    public void destroy() {
        super.destroy();
    }
}
